//引入express模块
const express = require('express');
//创建订单路由器对象
const o = express.Router();
//引入数据库连接池模块 
const oPool = require('../pool.js');

//1.添加按编号查看订单路由(get /get)  查看订单是否存在
o.get('/get',(req,res,next)=>{
	//1.1在后台打印查询字符串传递的数据
	console.log(req.query);
	let obj=req.query;
	//1.2执行SQL命令
	//在aa_order中查询订单
	if(obj.user_id){
		oPool.query('SELECT * FROM aa_order WHERE user_id=?',[obj.user_id],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"订单已存在","data":result});
			}else{
				res.send({"code":201,"msg":"订单不存在"});
			}
		});	
	}
	//在aa_order_detail中查询订单信息
	if(obj.order_id){
		oPool.query('SELECT * FROM aa_order_detail WHERE order_id=?',[obj.order_id],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印查询的结果
			console.log(result);
			if(result.length>0){
				res.send({"code":200,"msg":"订单信息已存在","data":result});
			}else{
				res.send({"code":201,"msg":"订单信息不存在"});
			}
		});
	}
});

//2.添加订单添加路由 (post /reg)
o.post('/reg',(req,res,next)=>{
	//2.1在后台打印流传递的数据
	console.log(req.body);
	let obj=req.body;
	//2.2判断添加订单内容是否为空
	let n=400;
	for(let k in obj){
		if(!obj[k]){
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//2.3执行SQL命令
	//2.3.1在aa_order中添加订单
	if(obj.user_id){				
		oPool.query('INSERT INTO aa_order SET ?',[obj],( err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印结果
			console.log(result);
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"注册失败"}); 
			}else{
				res.send({"code":200,"msg":"注册成功"});
			}
		});	
	}
	//2.3.2在aa_laptop中加入关于订单的信息
	if(obj.order_id){
		oPool.query('INSERT INTO aa_order_detail SET ?',[obj],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			//打印插入的商品信息
			console.log(result);
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"插入失败"});
			}else{
				res.send({"code":200,"msg":"插入成功"});
			}
		});	
	}
});

//3.添加订单删除路由('delete /:')
//3.1删除表aa_order中指定的订单('delete /a:oid')
o.delete('/a/:oid',(req,res,next)=>{
	//在后台打印路由传参传递的数据
	console.log(req.params);
	let obj=req.params;
	//执行SQL命令	
	oPool.query('DELETE FROM aa_order WHERE oid=?',[obj.oid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//打印result并查看affectedRows是否为0，为0则删除失败，否则删除成功
		console.log(result);
		if(result.affectedRows===0){
				res.send({"code":201,"msg":"删除失败"});
		}else{
			res.send({"code":200,"msg":"删除成功"});
		}
	});
});
//3.2删除表aa_order_detail中指定的订单('delete /b/:did')
o.delete('/b/:did',(req,res,next)=>{
	//在后台打印路由传参传递的数据
	console.log(req.params);
	let obj=req.params;
	//执行SQL命令	
	oPool.query('DELETE FROM aa_order_detail WHERE did=?',[obj.did],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//打印result并查看affectedRows是否为0，为0则删除失败，否则删除成功
		console.log(result);
		if(result.affectedRows===0){
			res.send({"code":201,"msg":"删除失败"});
		}else{
			res.send({"code":200,"msg":"删除成功"});
		}
	});	
});

//4.添加修改订单路由(put /)
o.put('/',(req,res,next)=>{
	//4.1在控制台打印流传递的数据
	console.log(req.body);
	let obj=req.body;
	//4.2判断订单修改的值是否为空，为空则跳出并报错
	let n=400;
	for(let k in obj){
		if(!obj[k]){
			res.send({"code":n++,"msg":k+"不能为空"});
			return;
		}
	}
	//4.3修改aa_order中的订单
	if(obj.oid){
		//执行SQL命令
		oPool.query('UPDATE aa_order SET ? WHERE oid=?',[obj,obj.oid],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			console.log(result);
			//在控制台打印result并判断affectedRows是否为0，为0则修改失败，否则修改成功
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"修改失败"});
			}else{
				res.send({"code":200,"msg":"修改成功"});
			}
		});	
	}
	//4.4修改aa_order_detail中的订单信息
	if(obj.did){
		//执行SQL命令
		oPool.query('UPDATE aa_order_detail SET ? WHERE did=?',[obj,obj.did],(err,result)=>{
			if(err){
				next(err);
				return;
			}
			console.log(result);
			//在控制台打印result并判断affectedRows是否为0，为0则修改失败，否则修改成功
			if(result.affectedRows===0){
				res.send({"code":201,"msg":"修改失败"});
			}else{
				res.send({"code":200,"msg":"修改成功"});
			}
		});	
	}
});

//5.添加查询订单路由(get /list)
//5.1查询订单(get /list1)
o.get('/list1',(req,res,next)=>{
	//5.1获取查询字符串传递的数据
	console.log(req.query);
	let obj=req.query;
	//5.2查询订单列表
	//判断
	if(!obj.pno) obj.pno=1;
	if(!obj.count) obj.count=5;
	//计算每页开始查询的值
	let start=(obj.pno-1)*obj.count;
	//将每页的数据量转为整形
	let size=parseInt(obj.count);
	//执行SQL命令
	oPool.query('SELECT * FROM aa_order LIMIT ?,?',[start,size],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//打印查询的结果
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询失败","data":result});
		}
	});
});
//5.1查询订单信息(get /list2)
o.get('/list2',(req,res,next)=>{
	//5.1获取查询字符串传递的数据
	console.log(req.query);
	let obj=req.query;
	//5.2查询订单列表
	//判断
	if(!obj.pno) obj.pno=1;
	if(!obj.count) obj.count=5;
	//计算每页开始查询的值
	let start=(obj.pno-1)*obj.count;
	//将每页的数据量转为整形
	let size=parseInt(obj.count);
	//执行SQL命令
	oPool.query('SELECT * FROM aa_order_detail LIMIT ?,?',[start,size],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//打印查询的结果
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询失败","data":result});
		}
	});
});

//6.添加随机查询订单路由(get /sui) 分两个路由 /sui1 /sui2
//6.1查询订单路由(get /sui1)
o.get('/sui1',(req,res,next)=>{
	//接受查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//执行SQL命令
	oPool.query('SELECT * FROM aa_order WHERE ?',[obj],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询订单成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询订单失败","data":result});
		}
	});
});
//6.2查询订单信息路由(get /sui2)
o.get('/sui2',(req,res,next)=>{
	//接受查询字符串传递的数据
	console.log(req.query);
	let obj = req.query;
	//执行SQL命令
	oPool.query('SELECT * FROM aa_order_detail WHERE ?',[obj],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.length>0){
			res.send({"code":200,"msg":"查询订单信息成功","data":result});
		}else{
			res.send({"code":201,"msg":"查询订单信息失败","data":result});
		}
	});
});

//7.添加获取所有订单信息(get /all)
//7.1获取订单(get /all1)
o.get('/all1',(req,res,next)=>{
	//获取查询字符串传递的数据
	console.log(req.query);
	//执行SQL命令
	oPool.query('SELECT * FROM aa_order',(err,result)=>{
		if(err){
			//出现错误，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"查询失败","data":result});
		}else{
			res.send({"code":200,"msg":"查询成功","data":result});
		}
	});
});
//7.2获取订单信息(get /olist2)
o.get('/all2',(req,res,next)=>{
	//获取查询字符串传递的数据
	console.log(req.query);
	//执行SQL命令
	oPool.query('SELECT * FROM aa_order_detail',(err,result)=>{
		if(err){
			//出现错误，将错误保存并传递给错误处理中间件
			next(err);
			return;
		}
		//判断查询是否成功
		console.log(result);
		if(result.affectedRows === 0){
			res.send({"code":201,"msg":"查询失败","data":result});
		}else{
			res.send({"code":200,"msg":"查询成功","data":result});
		}
	});
});

//导出路由器对象
module.exports = o;